package com.iwhalecloud.bss.kite.cucc.service.attr.handler;

import com.iwhalecloud.bss.kite.common.constant.ErrorConsts;
import com.iwhalecloud.bss.kite.common.util.KiteListUtils;
import com.iwhalecloud.bss.kite.common.util.KiteStringUtils;
import com.iwhalecloud.bss.kite.cucc.common.constant.LocalKeyConsts;
import com.iwhalecloud.bss.kite.manager.api.IQuerySceneInstService;
import com.iwhalecloud.bss.kite.manager.inst.KiteAttr;
import com.iwhalecloud.bss.kite.manager.inst.KiteInst;
import com.iwhalecloud.bss.kite.manager.inst.KiteOfferInst;
import com.iwhalecloud.bss.kite.service.attr.AttrData;
import com.iwhalecloud.bss.kite.service.attr.handler.AbstractAttrHandler;
import com.iwhalecloud.bss.kite.service.scene.attr.OperateInstAttrService;
import org.springframework.beans.factory.annotation.Autowired;

import java.math.BigDecimal;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.regex.Pattern;

/**
 * Description:天津附加商品订购个数属性联动
 * File Name:NumberOfOrdersAttrHandler
 * Modification History:
 * Date             Author      Version     Description
 * ------------------------------------------------------------------
 * 2022-3-12 17:52   邱萌         1.0       1.0 Version
 **/
public class NumberOfOrdersAttrHandler  extends AbstractAttrHandler<KiteInst, AttrData> {
    @Autowired
    private IQuerySceneInstService querySceneInstService;
    @Autowired
    OperateInstAttrService operateInstAttrService;

    @Override
    public void set(KiteAttr attr, KiteInst inst, String value, AttrData data) {
        super.set(attr, inst, value, data);
        if (KiteStringUtils.isEmpty(value)) {
            return;
        }
        //  订购个数
        String check = "^(\\d|[1-9]\\d+)(\\.\\d+)?$";
        boolean isMatch = Pattern.matches(check, value);
        // 是否为页面初始化渲染，是（T），跳过校验
        String isInitPageRenderFlag = querySceneInstService.getExtParameterValue(data.getSceneInstId(), "isInitPageRenderFlag");
        if (!isMatch&& !KiteStringUtils.isEqual(isInitPageRenderFlag, LocalKeyConsts.IFTRUE_T)) {
            ErrorConsts.BUSI_COMMON_ERROR.throwOut("订购个数输入不合法，必须大于等于0");
        }
        List<KiteOfferInst> kiteOfferInsts =  querySceneInstService.listMainOfferInstsByProdInstId(data.getCustId(),inst.getInstId());
        if(KiteListUtils.isNotEmpty(kiteOfferInsts)){
            List<KiteOfferInst> additionalOfferInsts= querySceneInstService.listAdditionalOfferInstsFromOfferInst(data.getCustId(),kiteOfferInsts.get(0).getOfferInstId());
            if(KiteListUtils.isNotEmpty(additionalOfferInsts)){
                for (KiteOfferInst kiteOfferInst:additionalOfferInsts) {
                    Map<String, KiteAttr> attrMap = kiteOfferInst.getAttrMap();
                    //获取单价属性
                    KiteAttr djAttr = attrMap.get(LocalKeyConsts.UNIT_PRICE);
                    if (KiteStringUtils.isNotEmpty(djAttr.getValue())) {
                        Set<String> keySet = attrMap.keySet();
                        BigDecimal djValueBd = new BigDecimal(djAttr.getValue());
                        BigDecimal gsValueBd = new BigDecimal(value);
                        BigDecimal attrValueBd = djValueBd.multiply(gsValueBd);
                        String attrValueString = attrValueBd.setScale(0,BigDecimal.ROUND_HALF_DOWN).toString();
                        if (keySet.contains(LocalKeyConsts.STANDARD_MONTHLY_RENT)) {
                            operateInstAttrService.setAttrValue(kiteOfferInst, LocalKeyConsts.STANDARD_MONTHLY_RENT, attrValueString, data.getSceneInstId(), null);
                        } else if (keySet.contains(LocalKeyConsts.PRICE)) {
                            operateInstAttrService.setAttrValue(kiteOfferInst, LocalKeyConsts.PRICE, attrValueString, data.getSceneInstId(), null);
                        }
                    }

                }

            }
        }
        super.set(attr, inst, value, data);
    }

}
